package com.wen.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wen.dto.BlogQuery;
import com.wen.dto.SearchBlog;
import com.wen.dto.ShowBlog;
import com.wen.pojo.Blog;
import com.wen.pojo.User;
import com.wen.service.BlogService;
import com.wen.service.TagService;
import com.wen.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    public void setTypeAndTag(Model model) {
        model.addAttribute("types", typeService.getAllType());//这里不是联表查询所有，联表查询的typemapper你还没写
        model.addAttribute("tags", tagService.getAllTag());
    }
    //查询所有的博客
    @GetMapping("/blogs")
    public String list(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<BlogQuery> allBlog = blogService.getAllBlogQuery();//搜索框为空白时，默认查询所有blog
        PageInfo<BlogQuery> pageInfo = new PageInfo<>(allBlog);
        model.addAttribute("pageInfo",pageInfo);
        setTypeAndTag(model);
        return "admin/blogs";
    }
    //搜索博客
    @PostMapping("/blogs/search")
    public String search(SearchBlog searchBlog,Model model,@RequestParam(defaultValue = "1" , value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<BlogQuery> blogQueryList = blogService.getBlogBySearch(searchBlog);//根据搜索条件查询blog
        PageInfo<BlogQuery> pageInfo = new PageInfo<>(blogQueryList);
        model.addAttribute("pageInfo",pageInfo);
        setTypeAndTag(model);
        model.addAttribute("message","查询成功");
        return "admin/blogs::blogList";
    }
    //跳转新增页面
    @GetMapping("/blogs/input")
    public String toAddBlogs(Model model){
        setTypeAndTag(model);
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }
    //博客新增
    @PostMapping("/blogs")
    public String addBlogs(Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        //设置blog的type
        blog.setType(typeService.getType(blog.getType().getId()));
        //设置blog的typeId属性
        blog.setTypeId(blog.getType().getId());
        //设置用户id
        blog.setUserId(blog.getUser().getId());
        int b = blogService.saveBlog(blog);
        if(b==0){
            attributes.addFlashAttribute("message","新增失败");
        }else{
            attributes.addFlashAttribute("message","新增成功");
        }
        return "redirect:/admin/blogs";
    }
    //跳转到更新页面
    @GetMapping("/blogs/{id}/input")
    public String toUpdateBlog(@PathVariable Long id,Model model){
        ShowBlog blogById = blogService.getBlogById(id);
        setTypeAndTag(model);
        model.addAttribute("blog",blogById);
        return "admin/blogs-update";
    }

    //编辑修改页面
    @PostMapping("/blogs/update")
    public String updateBlog(ShowBlog showBlog,RedirectAttributes attributes){
        int b = blogService.updateBlog(showBlog);
        if(b==0){
            attributes.addFlashAttribute("message","修改失败");
        }else {
            attributes.addFlashAttribute("message","修改成功");
        }
        return "redirect:/admin/blogs";
    }
    //删除博客
    @GetMapping("/blogs/{id}/delete")
    public String deleteBlogs(@PathVariable Long id,RedirectAttributes attributes){
        int b = blogService.deleteBlog(id);
        if(b==0){
            attributes.addFlashAttribute("message","删除失败");
        }else {
            attributes.addFlashAttribute("message","删除成功");
        }
        return "redirect:/admin/blogs";
    }

}
